public class Main {
    public static void main(String[] args) {

        int[] nums={1,-2,3,-2};
        Main main = new Main();
        main.maxSubarraySumCircular(nums);
    }
    //环形⼦数组的最⼤和
    public int maxSubarraySumCircular(int[] nums) {
        int n=nums.length;
        int sum=0;
        int[] f=new int[n+1];
        int[] g=new int[n+1];
        int fMax=Integer.MIN_VALUE;
        int gMin=Integer.MAX_VALUE;
        f[0]=0;
        g[0]=0;
        for(int i=1;i<=n;i++){
            sum+=nums[i-1];
            f[i]=Math.max(f[i-1]+nums[i-1],nums[i-1]);
            fMax=Math.max(fMax,f[i]);
            g[i]=Math.min(g[i-1]+nums[i-1],nums[i-1]);
            gMin=Math.min(gMin,g[i]);
        }
        return sum == gMin ? fMax : Math.max(fMax, sum - gMin);
    }

    //乘积最大子数组
    public int maxProduct(int[] nums) {
        int n=nums.length;
        int[] f=new int[n+1];
        int[] g=new int[n+1];
        f[0]=1;
        g[0]=1;
        int ret=Integer.MIN_VALUE;
        for(int i=1;i<=n;i++){
            f[i]=Math.max(nums[i-1],Math.max(f[i-1]*nums[i-1],nums[i-1]*g[i-1]));
            g[i]=Math.min(nums[i-1],Math.min(f[i-1]*nums[i-1],g[i-1]*nums[i-1]));
            ret=Math.max(ret,f[i]);
        }
        return ret;
    }
}